// pages/Home/CommonProblem/CommonProblem.js
import { queryAppInforMation } from '../../../api/brand';
import { throttle } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    page: 0,
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getProblemList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
      dataList: [],
    })
    this.getProblemList();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    console.log('上拉加载')
    this.getProblemList();
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 1;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取常见问题
  getProblemList() {
    let _this = this;
    queryAppInforMation({
        infoType: 12,
        size: 3
      })
      .then(res => {
        if (res.success) {
          var list = _this.data.dataList;
        if(_this.data.page == 0){
          list = res.data.content;
        }else{
          list = list.concat(res.data.content);
        }
        _this.setData({
          dataList: list,
          total: res.data.totalElements,
          page: _this.data.page+1,
        })
        if(_this.data.page * 10 >= res.data.totalElements){
          _this.setData({
            isReachBottom: false,
            noMore: true,
            loading: false,
            loadingFailed: false,
          })
        }else{
          _this.setData({
            isReachBottom: true,
            noMore: false,
            loading: false,
            loadingFailed: false,
          })
        }
        } else {
          wx.showToast({
            title: res.errMessage,
            icon: 'none'
          })
        }
      })
  },
})